package com.vdm.action;

import com.vdm.model.UserInfo;
import com.vdm.model.VehicleInfo;
import com.vdm.service.VehicleService;
import com.vdm.util.Constants;
import com.vdm.util.ErrorConstants;
import com.vdm.util.ReturnMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/vehicle")
public class VehicleController {

    @Autowired
    VehicleService vehicleService;

    @RequestMapping(value = "/toVehicleList")
    public String toUserList(){return "vehicle/vehicleList";}

    @ResponseBody
    @RequestMapping(value = "/vehicleList",method = RequestMethod.GET)
    public Map<String,Object> vehicleList(){
        List<VehicleInfo> userList = vehicleService.vehicleInfoList();
        int count = vehicleService.getVehicleInfoCount();
        Map<String,Object> retMap = new HashMap<String,Object>();
        retMap.put("code",0);
        retMap.put("msg","");
        retMap.put("count",count);
        retMap.put("data",userList);
        return retMap;
    }

    @ResponseBody
    @RequestMapping(value="/updateVehicleStatus",method=RequestMethod.POST)
    public Map<String,Object> updateUserStatus(@RequestParam(value = "list[]")List<Integer> ids, Integer type){
        if(vehicleService.updateVehicleInfoStatus(ids, type)>0){
            return ReturnMapUtil.getOKResult();
        }else{
            return ReturnMapUtil.getErrorResult(ErrorConstants.PARAMETER_ERROR);
        }
    }

    @ResponseBody
    @RequestMapping(value="/updateVehicleByPk",method=RequestMethod.POST)
    public Map<String,Object> updateVehicleByPk(VehicleInfo vehicleInfo){
        if(vehicleService.updateByPk(vehicleInfo)>0){
            return ReturnMapUtil.getOKResult();
        }else{
            return ReturnMapUtil.getErrorResult(ErrorConstants.PARAMETER_ERROR);
        }
    }

    @RequestMapping(value = "/toAddVehicle")
    public String toAddUser(){return "vehicle/addVehicle";}

    @RequestMapping(value = "/addVehicle" , method = RequestMethod.POST)
    public String addUser(VehicleInfo vehicleInfo, HttpSession session){
        UserInfo user = (UserInfo) session.getAttribute(Constants.CURRENT_USER);
        vehicleInfo.setCreateBy(user.getLoginName());
        vehicleService.insertSelective(vehicleInfo);
        return "redirect:toVehicleList";
    }

}
